package icu.qx.qc.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.qc.domain.QcQualityProject;
import icu.qx.qc.service.IQcQualityProjectService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import icu.qx.common.utils.CodeUtils;

/**
 * 质检项目Controller
 *
 * @author ruoyi
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/qc/project")
public class QcQualityProjectController extends BaseController {
    @Autowired
    private IQcQualityProjectService qcQualityProjectService;
    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询质检项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QcQualityProject qcQualityProject) {
        startPage();
        List<QcQualityProject> list = qcQualityProjectService.selectQcQualityProjectList(qcQualityProject);
        return getDataTable(list);
    }

    /**
     * 导出质检项目列表
     */
    @RequiresPermissions("qc:project:export")
    @Log(title = "质检项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcQualityProject qcQualityProject) {
        List<QcQualityProject> list = qcQualityProjectService.selectQcQualityProjectList(qcQualityProject);
        ExcelUtil<QcQualityProject> util = new ExcelUtil<QcQualityProject>(QcQualityProject.class);
        util.exportExcel(response, list, "质检项目数据");
    }

    /**
     * 获取质检项目详细信息
     */
    @GetMapping(value = "/{qualityCode}")
    public AjaxResult getInfo(@PathVariable("qualityCode") String qualityCode) {
        return success(qcQualityProjectService.selectQcQualityProjectByQualityCode(qualityCode));
    }

    /**
     * 新增质检项目
     */
    @RequiresPermissions("qc:project:add")
    @Log(title = "质检项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QcQualityProject qcQualityProject) {
        qcQualityProject.setQualityCode(codeUtils.generateCode("qc_quality_project"));
        qcQualityProject.setCreateBy(SecurityUtils.getUsername());
        // 获取当前时间
        Date currentTime = new Date();
        qcQualityProject.setCreateTime(currentTime);
        return toAjax(qcQualityProjectService.insertQcQualityProject(qcQualityProject));
    }

    /**
     * 修改质检项目
     */
    @Log(title = "质检项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QcQualityProject qcQualityProject) {
        qcQualityProject.setUpdateBy(SecurityUtils.getUsername());
        // 获取当前时间
        Date currentTime = new Date();
        qcQualityProject.setUpdateTime(currentTime);
        return toAjax(qcQualityProjectService.updateQcQualityProject(qcQualityProject));
    }

    /**
     * 删除质检项目
     */
    @RequiresPermissions("qc:project:remove")
    @Log(title = "质检项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qualityCodes}")
    public AjaxResult remove(@PathVariable String[] qualityCodes) {
        return toAjax(qcQualityProjectService.deleteQcQualityProjectByQualityCodes(qualityCodes));
    }
}
